//app.js 
App({
	onLaunch: function(option) {
        const updateManager = tt.getUpdateManager()

        updateManager.onCheckForUpdate(function(res) {
            // 请求完新版本信息的回调
            console.log(res.hasUpdate)
        })
        updateManager.onUpdateReady(function() {
            tt.showModal({
                title: '更新提示',
                content: '新版本已经准备好，是否重启应用？',
                success: function(res) {
                    if (res.confirm) {
                        // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                        updateManager.applyUpdate()
                    }
                }
            })
        })

        updateManager.onUpdateFailed(function() {
            // 新版本下载失败
        })
        if(option.query.share_user_id){
            tt.setStorage({
                data: option.query.share_user_id,
                key: 'share_user_id',
            })
            this.globalData.share_user_id = option.query.share_user_id
        }else{
            tt.setStorage({
                data: '0',
                key: 'share_user_id',
            })
            this.globalData.share_user_id = "0"
        }
		this.login()
		// tt.clearStorage()
		tt.getSystemInfo({
			success(res) {
				tt.setStorageSync('statusBarHeight', res.statusBarHeight)
				if (res.platform == "ios") {
					tt.setStorageSync('ios', true)
				} else {
					tt.setStorageSync('ios', false)
				}
			}
		})
        this.huoqu()
        this.versionReview()
	},
	login: function() {
		tt.login({
			success: (res) => {
				this.request(res.code)
			}, 
			fail: (err) => {}, 
		});
	},
	request: function(data) { 
        let that = this
		let param = 'js_code=' + data + '&app_id=tt687924844876863140&app_secret=8R13aWFd-iKRtt8wip1-gw' 
		tt.request({
			url: 'https://open.kuaishou.com/oauth2/mp/code2session?' + param,
			method: 'POST',
			data: {},
			success: res => {
				tt.setStorageSync('option_id', res.data.open_id) 
			}, 
			fail: err => { 
				console.log(err) 
			} 
		})
	},
    huoqu:function(){
        tt.getSystemInfo({
            success: e => {
                //状态栏
                let statusBarHeight= e.statusBarHeight;
                // 右上角胶囊
                let menuButton = tt.getMenuButtonBoundingClientRect();
                //胶囊宽度加上左右边距的新宽度【即自定义导航栏时，左边元素的margin-right的值】
                menuButton.extendWidth=menuButton.width+2*(e.windowWidth-menuButton.right);
                menuButton.marginRight=e.windowWidth-menuButton.right;//胶囊的右边距。用于设置返回的左边距。对称
                // 保存
                this.globalData.statusBarHeight =statusBarHeight
                this.globalData.menuButton = menuButton;  
                this.globalData.customNavBarHeight= menuButton.bottom + menuButton.top - statusBarHeight;
            }
        })
    },
    versionReview:function () {
        let that = this
        tt.request({
			url: 'https://t.doule.shop/api/common/versionReview',
			method: 'GET',
			data: {"appid":"tt687924844876863140","version":that.globalData.edition},
			success: res => {
				console.log(res)
                that.globalData.versionNumber = res.data.data.review_status
			},
			fail: err => {
				console.log(err)
			}
		})
	},
 // 全局变量
    globalData: {
        userInfo: null,
        //自定义导航相关的
        statusBarHeight:null,//状态栏高度
        menuButton:null,//胶囊相关数据 @obj,{width,height,top,left,right,bottom}
        customNavBarHeight:null,//自定义导航栏的高度【导航栏包括了状态栏】
        userId: null,
        share_user_id:'0',
        show:false,
        bind_promote:'',
        edition:'1.0.1',
        versionNumber:false
    },
    
}) 
